package cn.wolfcode.trip.app.web.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.AddComment;
import cn.wolfcode.trip.base.domain.StrategyAttention;
import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.CollectQueryObject;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.service.IAddCommentService;
import cn.wolfcode.trip.base.service.ICollectService;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


@Controller
@RequestMapping("/strategyComments")
public class StrategyCommentController {
    @Autowired
    private IStrategyCommentService iStrategyCommentService;
    @Autowired
    private IAddCommentService iAddCommentService;
    @Autowired
    private ICollectService collectService;

   /* //在index页面攻略点评轮播
    @GetMapping("subString")
    public Object listStrategyComments() {
        return iStrategyCommentService.getAllStrategyComments();
    }*/

    //获取指定攻略中的所有评论
    @GetMapping
    @ResponseBody
    public Object listStrategyComments(StrategyCommentQueryObject qo) {
        PageInfo pageInfo = iStrategyCommentService.queryForApp(qo);
        return pageInfo;
    }




    //获取某个评论详情
    @GetMapping("/comments/{commentId}")
    @ResponseBody
    public Object get(@PathVariable("commentId") Long strategyCommentId) {
        StrategyComment comment = iStrategyCommentService.getStrategyCommentById(strategyCommentId);
        return comment;
    }


    //获取指定攻略id的所有评论
    @GetMapping("/mine/{userId}")
    @ResponseBody
    public Object listStrategyCommentByUserId(StrategyCommentQueryObject qo) {
        PageInfo pageInfo = iStrategyCommentService.queryForApp(qo);
        return pageInfo;
    }
    //通过点评id获取点评详情
    @GetMapping("/{commentId}")
    @ResponseBody
    public Object listCommentsByUserId(StrategyCommentQueryObject qo){
        PageInfo pageInfo = iStrategyCommentService.queryForApp(qo);
        return pageInfo;
    }

    //获取所有登录用户被评论的数量
    @GetMapping("/getStrategyCount")
    @ResponseBody
    public Object listStrategyCommentCount() {
        int count = iStrategyCommentService.getStrategyCommentCount(UserContext.getCurrentUser().getId());
        return count;
    }
    //根据用户id查询该用户评论的所有评论内容
    @GetMapping("/user/{id}")
    @ResponseBody
    public Object getStrategyCommentByUserId(@PathVariable("id") Long id){
        Map<String, Object> comments = iStrategyCommentService.getStrategyCommentsByUserId(id);
        return comments;
    }
    //在评论创建时,默认子评论初始值为0
    @PutMapping("resetCount")
    @ResponseBody
    public Object resetCount() {
        JSONResult result = new JSONResult();
        User user = UserContext.getCurrentUser();
        try {
            iStrategyCommentService.resetCount(user.getId());
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @PostMapping("/{commentId}/addComment")
    @ResponseBody
    public Object save(AddComment comment) {
        JSONResult result = new JSONResult();
        try {
            iAddCommentService.save(comment);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("保存数据异常");
        }
        return result;
    }

    //评论中的点赞
    @GetMapping("like")
    @ResponseBody
    public Object isLike(StrategyAttention attention) {
        JSONResult result = new JSONResult();
        try {
            boolean b = iStrategyCommentService.iselectCollect(attention);
            /*int count = iStrategyCommentService.selectLikeCountByCommentId(attention);
            result.setData(count);*/
            result.setSuccess(b);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            iStrategyCommentService.deleteCollect(attention);
            result.mark(e.getMessage());
        }
        return result;
    }

    //点赞回显
    @GetMapping("/chose")
    @ResponseBody
    public Object chose(StrategyAttention attention) {
        JSONResult result = new JSONResult();
        try {
            StrategyAttention collect = iStrategyCommentService.getCollect(attention);
            int count = iStrategyCommentService.selectLikeCountByCommentId(attention);
            if (count == 0) {
                result.setSuccess(false);
            } else {
                result.setSuccess(true);
            }
            result.setData(count);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    //取消点赞
    @DeleteMapping("/deleteChose/{commentId}/{userId}")
    @ResponseBody
    public Object deleteChose(@PathVariable("commentId") Long commentId,
                              @PathVariable("userId")Long userId ) {
        JSONResult result = new JSONResult();
        try {
            iStrategyCommentService.deleteChose(commentId, userId);
            result.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("网络异常,操作失败");
        }
        return result;
    }

    //进行点赞
    @PostMapping("/insertChose/{commentId}/{userId}")
    @ResponseBody
    public Object insertChose(@PathVariable("commentId") Long commentId,
                              @PathVariable("userId")Long userId ) {
        JSONResult result = new JSONResult();
        try {
            iStrategyCommentService.insertChose(commentId, userId);
            result.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("网络异常,操作失败");
        }
        return result;
    }

    //收藏功能begin....................................
    @PostMapping("/{userId}")
    @ResponseBody
    public Object addCollect(CollectQueryObject qo){
        JSONResult json = new JSONResult();
        try {
            collectService.addCommentCollect(qo);
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }
    //取消收藏
    @DeleteMapping("/{userId}")
    @ResponseBody
    public Object removeCollect(CollectQueryObject qo){
        JSONResult json = new JSONResult();
        try {
            collectService.removeCommentCollect(qo);
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }
    //为点评的收藏数计数
    @GetMapping("/collectCount")
    @ResponseBody
    public Object countCollect(Long commentId){
        Integer num = collectService.countCommentCollect(commentId);
        return num;
    }
    //判断该点评是否收藏
    @GetMapping("/{userId}/{commentId}")
    @ResponseBody
    public Object isCollect(@PathVariable Long userId,@PathVariable Long commentId){
        Integer count = collectService.commentIsCollect(userId,commentId);
        return count!=0;
    }
    //收藏夹列表
    @GetMapping("/commentCollect")
    @ResponseBody
    public Object selectCollectByUserId(Long userId){
        return collectService.selectCommentCollectByUserId(userId);
    }
    //end...............................................


}
